/**
 * Copyright &copy; 2012-2014 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.book.web;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.book.entity.BkType;
import com.thinkgem.jeesite.modules.book.service.BookTypeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 流程个人任务相关Controller
 *
 * @author ThinkGem
 * @version 2013-11-03
 */
@Controller
@RequestMapping(value = "${adminPath}/book/bktype")
public class BookTypeController extends BaseController{

    @Autowired
    private BookTypeService bookTypeService;

    @ModelAttribute
    public BkType get(@RequestParam(required = false) String id){
        if(StringUtils.isNotBlank(id)){
            return bookTypeService.get(id);
        }
        else{
            return new BkType();
        }
    }

    @RequiresPermissions("book:booktype:view")
    @RequestMapping(value = {"list", ""})
    public String list(){
        return "modules/book/bookTypeList";
    }

    /**
     * 获取分类信息json
     */
    @RequestMapping(value="json",method = RequestMethod.GET)
    @ResponseBody
    public List<BkType> bktypeInfoList() {
        List<BkType> bkTypes=bookTypeService.getAll();
        return bkTypes;
    }

    /**
     * 添加分类跳转
     *
     * @param model
     */
    @RequestMapping(value = "create", method = RequestMethod.GET)
    public String createForm(Model model) {
        model.addAttribute("bkType", new BkType());
        model.addAttribute("action", "create");
        return "modules/book/booktypeForm";
    }


    /**
     * 添加分类信息
     *
     * @param bkType
     * @param model
     */
    @RequestMapping(value = "create", method = RequestMethod.POST)
    @ResponseBody
    public String create(@Valid BkType bkType, Model model) {
        bookTypeService.save(bkType);
        return "success";
    }


    /**
     * 删除分类信息
     *
     * @param
     * @return
     */
    @RequestMapping(value = "delete")
    @ResponseBody
    public String delete(HttpServletRequest request) {

        //首先要判断是否作为父类，如果是父类不允许删除

        BkType bkType =  bookTypeService.get(request.getParameter("id"));

        List<BkType> list = bookTypeService.getbyPid(request.getParameter("id"));

        if(list.size() !=0){

            return "faile";

        }else {

            bookTypeService.deleteone(bkType);
        }


        return "success";
    }




    /**
     * 修改分类信息跳转
     *
     * @param id
     * @param model
     * @return
     */
    @RequestMapping(value = "update/{id}", method = RequestMethod.GET)
    public String updateForm(@PathVariable("id") String id, Model model) {
        model.addAttribute("bkType", bookTypeService.get(id));
        model.addAttribute("action", "update");
        return "modules/book/booktypeForm";
    }

    /**
     * 修改分类信息
     *
     * @param bkType
     * @param model
     * @return
     */
    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ResponseBody
    public String update(@Valid @ModelAttribute @RequestBody BkType bkType,Model model) {

        bookTypeService.update(bkType);

        return "success";
    }

    /**
     * 获取书籍分类JSON数据。
     * @param grade 显示级别
     * @param response
     * @return
     */
    @RequiresPermissions("user")
    @ResponseBody
    @RequestMapping(value = "treeData")
    public List<Map<String, Object>> treeData(HttpServletResponse response) {
        List<Map<String, Object>> mapList = Lists.newArrayList();
        List<BkType> list=bookTypeService.getAll();
        for(BkType e : list){
            Map<String, Object> map = Maps.newHashMap();
            map.put("id", e.getId());
            map.put("pId", e.getPid());
            map.put("classcode", e.getTypName());
            map.put("name", e.getTypDisn()+"("+e.getTypName()+")");
            map.put("open", "false");
            mapList.add(map);
        }
        return mapList;
    }
}